maven surefire-plugin介绍

  • 查询surefire-plugins的资料的时候不好找,故整理
  • 方便查询+review其使用方法

Documents

normal case

  • 主要参考官网场景,结合使用心得分析

Using TestNG

Using JUnit

Using Pojo Tests

Skipping Tests

Skip after failure

Inclusions and exclusions of Tests

Running a single test

re-run failing test

class loading and forking

  • 待补充

debugging tests

  • 待补充

using system properties

  • 待补充

configuring the classpath

  • 待补充

selecting providers

  • 待补充

new error summary

  • 待补充

fork options and parallel test excution

using console logs

shutdown of forked JVM

FAQ

如何优化测试效率

- 资源允许情况下,配置多VM
- 配置多线程
- 调优VM参数
- 减少大资源的重复创建,如db连接池,单元测试时c3p0换成jdbc,然后单独测试c3p0即可

如何指定单个测试用例/部分测试用例

- mvn test -Dtest=
- 后面值是类的完全路径名

Perm溢出,如何处理

- 参考configuration中的argLine
- http://stackoverflow.com/questions/23260057/the-forked-vm-terminated-without-saying-properly-goodbye-vm-crash-or-system-exi
- 亦可修改MVN全局配置:

configuration可配置值列表(base on surefire-v2.19.1)

<additionalClasspathElements></additionalClasspathElements>

<argLine></argLine>
    - 可用于配置jvm的相关参数

<basedir></basedir>

<childDelegation></childDelegation>

<classesDirectory></classesDirectory>

<classpathDependencyExcludes></classpathDependencyExcludes>

<classpathDependencyScopeExclude></classpathDependencyScopeExclude>

<debugForkedProcess></debugForkedProcess>

<dependenciesToScan></dependenciesToScan>

<detail></detail>

<disableXmlReport></disableXmlReport>

<enableAssertions></enableAssertions>

<environmentVariables></environmentVariables>

<excludedGroups></excludedGroups>

<excludes></excludes>

<excludesFile></excludesFile>

<failIfNoSpecifiedTests></failIfNoSpecifiedTests>

<failIfNoTests></failIfNoTests>

<forkCount></forkCount>
    - Option to specify the number of VMs to fork in parallel in order to execute the tests. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point value are only accepted together with "C". If set to "0", no VM is forked and all tests are executed within the main process.
    - Example values: "1.5C", "4"
    - The system properties and the argLine of the forked processes may contain the place holder string ${surefire.forkNumber}, which is replaced with a fixed number for each of the parallel forks, ranging from 1 to the effective value of forkCount times the maximum number of parallel Surefire executions in maven parallel builds, i.e. the effective value of the -T command line argument of maven core.

<forkMode></forkMode>
    - Option to specify the forking mode. Can be "never", "once", "always", "perthread". "none" and "pertest" are also accepted for backwards compatibility. "always" forks for each test-class. "perthread" will create threadCount parallel forks, each executing one test-class. See also parameter reuseForks.
    - 现在啊建议用reuseFork代替

<forkedProcessTimeoutInSeconds></forkedProcessTimeoutInSeconds>
    - Kill the forked test process after a certain number of seconds. If set to 0, wait forever for the process, never timing out.

<goal></goal>

<groups></groups>

<includes></includes>

<includesFile></includesFile>

<indentSize></indentSize>

<junitArtifactName></junitArtifactName>

<jvm></jvm>

<lineLength></lineLength>

<objectFactory></objectFactory>

<parallel></parallel>
    - (TestNG provider) When you use the parallel attribute, TestNG will try to run all your test methods in separate threads, except for methods that depend on each other, which will be run in the same thread in order to respect their order of execution. <p/> (JUnit 4.7 provider) Supports values "classes"/"methods"/"both" to run in separate threads, as controlled by threadCount.
    - Since version 2.16 (JUnit 4.7 provider), the value "both" is <strong>DEPRECATED</strong>. Use <strong>"classesAndMethods"</strong> instead.
    - Since version 2.16 (JUnit 4.7 provider), additional vales are available "suites"/"suitesAndClasses"/"suitesAndMethods"/"classesAndMethods"/"all".
    - 配合treadCount使用

<parallelOptimized></parallelOptimized>

<parallelTestsTimeoutForcedInSeconds></parallelTestsTimeoutForcedInSeconds>

<parallelTestsTimeoutInSeconds></parallelTestsTimeoutInSeconds>

<perCoreThreadCount></perCoreThreadCount>

<printSummary></printSummary>

<properties></properties>

<redirectTestOutputToFile></redirectTestOutputToFile>

<remoteRepositories></remoteRepositories>

<reportFormat></reportFormat>

<reportNameSuffix></reportNameSuffix>

<reportsDirectory></reportsDirectory>

<rerunFailingTestsCount></rerunFailingTestsCount>

<reuseForks></reuseForks>
    -Indicates if forked VMs can be reused. If set to "false", a new VM is forked for each test class to be executed. If set to "true", up to forkCount VMs will be forked and then reused to execute all tests.
    -配合forkCount

<runOrder></runOrder>

<shutdown></shutdown>

<skip></skip>

<skipAfterFailureCount></skipAfterFailureCount>
    -出现n个测试用例失败后,会跳过剩余测试用例并终止测试
    -需要JUnit4.0+,TestNG5.10+
    -可参考:http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/skip-after-failure.html

<skipExec></skipExec>

<skipTests></skipTests>

<suiteXmlFiles></suiteXmlFiles>

<systemProperties></systemProperties>

<systemPropertiesFile></systemPropertiesFile>

<systemPropertyVariables></systemPropertyVariables>

<test></test>

<testClassesDirectory></testClassesDirectory>

<testFailureIgnore></testFailureIgnore>

<testNGArtifactName></testNGArtifactName>

<testSourceDirectory></testSourceDirectory>

<threadCount></threadCount>
    - 配合parallel使用
    - (TestNG/JUnit 4.7 provider) The attribute thread-count allows you to specify how many threads should be allocated for this execution. Only makes sense to use in conjunction with the parallel parameter.


<threadCountClasses></threadCountClasses>

<threadCountMethods></threadCountMethods>

<threadCountSuites></threadCountSuites>

<trimStackTrace></trimStackTrace>

<useFile></useFile>

<useManifestOnlyJar></useManifestOnlyJar>

<useSystemClassLoader></useSystemClassLoader>

<useUnlimitedThreads></useUnlimitedThreads>

<workingDirectory></workingDirectory>